using System;
using System.Collections.Generic;
using System.Text;
using FrameworkX.Application.Security.Objects;
using FrameworkX.TinyCms.Core.Service;
using FrameworkX.ObjectSpace;

namespace FrameworkX.TinyCms.Core.Config
{
    public class SettingService : IService
    {
        Database Database;
        public void SetSetting(string key, string value)
        {
            Database.Delete(typeof(SystemConfigurationSetting), key);
            SystemConfigurationSetting setting = new SystemConfigurationSetting();
            setting.SettingKey = key;
            setting.Value = value;
            Database.Insert(setting);
        }

        public string GetSetting(string key)
        {
            SystemConfigurationSetting setting = Database.Select<SystemConfigurationSetting>(key);
            if (setting == null)
            {
                throw new InvalidOperationException("Setting key not found");
            }
            else
            {
                return setting.Value;
            }            
        }

        public string GetSetting(string key, string defaultValue)
        {
            SystemConfigurationSetting setting = Database.Select<SystemConfigurationSetting>(key);
            if (setting == null)
            {
                return defaultValue;
            }
            else
            {
                return setting.Value;
            }
        }


    }
}
